package com.cn.crmsystem.service.censusService.impl;

import com.cn.crmsystem.mapper.censusMapper.MoneyCensusMapper;
import com.cn.crmsystem.pojo.ResultJson;
import com.cn.crmsystem.pojo.User;
import com.cn.crmsystem.service.censusService.MoneyCensusService;
import com.cn.crmsystem.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
//from ldk
@Service
public class MoneyCensusServiceImpl implements MoneyCensusService {
    @Autowired
    private MoneyCensusMapper moneyCensusMapper;

    @Override
    public ResultJson moneyCensus(User user,String firstSelect, String secondSelect) {
        Map<String,Object> map=new HashMap<>();
        ResultJson resultJson=new ResultJson();
        if(firstSelect!=null &&  firstSelect.equals("nowDay")){
            map.put("nowDay", DateUtil.dateFormatString(new Date(),"yyyy-MM-dd"));
        }else if(firstSelect!=null && firstSelect.equals("nowMonth")){
            map.put("nowMonth", DateUtil.dateFormatString(new Date(),"yyyy-MM-dd"));
        }else if(firstSelect!=null && firstSelect.equals("nowYear")){
            map.put("nowYear",DateUtil.dateFormatString(new Date(),"yyyy-MM-dd"));
        }else{   //2020-09-07--2020-09-08
            map.put("s_time",firstSelect.split("--")[0]);
            map.put("e_time",firstSelect.split("--")[1]);
        }
        if(secondSelect!=null && secondSelect.equals("myself")){
            map.put("userId",user.getId());
        }else  if(secondSelect!=null && secondSelect.equals("myDepartment")){
            map.put("deptId",user.getDept_id());
        }
        List<Map<String,Object>> mapList=moneyCensusMapper.moneyCensus(map);
        if(mapList!=null){
            return resultJson.getSuccess("成功",mapList);
        }else {
            return resultJson.getFail("失败");
        }
    }

    @Override
    public ResultJson returnMoneyCensus(User user, String firstSelect, String secondSelect) {
        Map<String,Object> map=new HashMap<>();
        ResultJson resultJson=new ResultJson();
        if(firstSelect!=null &&  firstSelect.equals("nowDay")){
            map.put("nowDay", DateUtil.dateFormatString(new Date(),"yyyy-MM-dd"));
        }else if(firstSelect!=null && firstSelect.equals("nowMonth")){
            map.put("nowMonth", DateUtil.dateFormatString(new Date(),"yyyy-MM"));
        }else if(firstSelect!=null && firstSelect.equals("nowYear")){
            map.put("nowYear",DateUtil.dateFormatString(new Date(),"yyyy-MM-dd"));
        }else{   //2020-09-07--2020-09-08
            map.put("s_time",firstSelect.split("--")[0]);
            map.put("e_time",firstSelect.split("--")[1]);
        }
        if(secondSelect!=null && secondSelect.equals("myself")){
            map.put("userId",user.getId());
        }else  if(secondSelect!=null && secondSelect.equals("myDepartment")){
            map.put("deptId",user.getDept_id());
        }
        List<Map<String,Object>> mapList=moneyCensusMapper.returnMoneyCensus(map);
        if(mapList!=null){
            return resultJson.getSuccess("成功",mapList);
        }else {
            return resultJson.getFail("失败");
        }
    }

    //排行榜
    //回款
    @Override
    public ResultJson rankingReturnMoney(User user, String firstSelect, String secondSelect) {
        Map<String,Object> map=new HashMap<>();
        ResultJson resultJson=new ResultJson();
        if(firstSelect!=null){
            if(firstSelect.equals("nowDay")){
                map.put("nowDay", DateUtil.dateFormatString(new Date(),"yyyy-MM-dd"));
            }else if(firstSelect.equals("nowMonth")){
                map.put("nowMonth", DateUtil.dateFormatString(new Date(),"yyyy-MM"));
            }else  if(firstSelect.equals("nowYear")){
                map.put("nowYear", DateUtil.dateFormatString(new Date(),"yyyy"));
            }else{
                map.put("s_time",firstSelect.split("--")[0]);
                map.put("e_time",firstSelect.split("--")[1]);
            }
        }
        if(secondSelect!=null){
            if(secondSelect!=null && secondSelect.equals("myself")){
                map.put("userId",user.getId());
            }else  if(secondSelect!=null && secondSelect.equals("myDepartment")){
                map.put("deptId",user.getDept_id());
            }
        }
        List<Map<String,Object>> mapList=moneyCensusMapper.rankingReturnMoney(map);
        if(mapList!=null){
            return resultJson.getSuccess("成功",mapList);
        }else {
            return resultJson.getFail("失败");
        }
    }
    //2 合同金额
    @Override
    public ResultJson rankingcontractMoney(User user, String firstSelect, String secondSelect) {
        Map<String,Object> map=new HashMap<>();
        ResultJson resultJson=new ResultJson();
        if(firstSelect!=null){
            if(firstSelect.equals("nowDay")){
                map.put("nowDay", DateUtil.dateFormatString(new Date(),"yyyy-MM-dd"));
            }else if(firstSelect.equals("nowMonth")){
                map.put("nowMonth", DateUtil.dateFormatString(new Date(),"yyyy-MM"));
            }else  if(firstSelect.equals("nowYear")){
                map.put("nowYear", DateUtil.dateFormatString(new Date(),"yyyy"));
            }else{
                map.put("s_time",firstSelect.split("--")[0]);
                map.put("e_time",firstSelect.split("--")[1]);
            }
        }
        if(secondSelect!=null){
            if(secondSelect!=null && secondSelect.equals("myself")){
                map.put("userId",user.getId());
            }else  if(secondSelect!=null && secondSelect.equals("myDepartment")){
                map.put("deptId",user.getDept_id());
            }
        }
        List<Map<String,Object>> mapList=moneyCensusMapper.rankingcontractMoney(map);
        if(mapList!=null){
            return resultJson.getSuccess("成功",mapList);
        }else {
            return resultJson.getFail("失败");
        }
    }
    //3 合同个数
    @Override
    public ResultJson rankingcontractCountMoney(User user, String firstSelect, String secondSelect) {
        Map<String,Object> map=new HashMap<>();
        ResultJson resultJson=new ResultJson();
        if(firstSelect!=null){
            if(firstSelect.equals("nowDay")){
                map.put("nowDay", DateUtil.dateFormatString(new Date(),"yyyy-MM-dd"));
            }else if(firstSelect.equals("nowMonth")){
                map.put("nowMonth", DateUtil.dateFormatString(new Date(),"yyyy-MM"));
            }else  if(firstSelect.equals("nowYear")){
                map.put("nowYear", DateUtil.dateFormatString(new Date(),"yyyy"));
            }else{
                map.put("s_time",firstSelect.split("--")[0]);
                map.put("e_time",firstSelect.split("--")[1]);
            }
        }
        if(secondSelect!=null){
            if(secondSelect!=null && secondSelect.equals("myself")){
                map.put("userId",user.getId());
            }else  if(secondSelect!=null && secondSelect.equals("myDepartment")){
                map.put("deptId",user.getDept_id());
            }
        }
        List<Map<String,Object>> mapList=moneyCensusMapper.rankingcontractCountMoney(map);
        if(mapList!=null){
            return resultJson.getSuccess("成功",mapList);
        }else {
            return resultJson.getFail("失败");
        }
    }
    //4 新增客户
    @Override
    public ResultJson rankingnewaddCustomer(User user, String firstSelect, String secondSelect) {
        Map<String,Object> map=new HashMap<>();
        ResultJson resultJson=new ResultJson();
        if(firstSelect!=null){
            if(firstSelect.equals("nowDay")){
                map.put("nowDay", DateUtil.dateFormatString(new Date(),"yyyy-MM-dd"));
            }else if(firstSelect.equals("nowMonth")){
                map.put("nowMonth", DateUtil.dateFormatString(new Date(),"yyyy-MM"));
            }else  if(firstSelect.equals("nowYear")){
                map.put("nowYear", DateUtil.dateFormatString(new Date(),"yyyy"));
            }else{
                map.put("s_time",firstSelect.split("--")[0]);
                map.put("e_time",firstSelect.split("--")[1]);
            }
        }
        if(secondSelect!=null){
            if(secondSelect!=null && secondSelect.equals("myself")){
                map.put("userId",user.getId());
            }else  if(secondSelect!=null && secondSelect.equals("myDepartment")){
                map.put("deptId",user.getDept_id());
            }
        }
        List<Map<String,Object>> mapList=moneyCensusMapper.rankingnewaddCustomer(map);
        if(mapList!=null){
            return resultJson.getSuccess("成功",mapList);
        }else {
            return resultJson.getFail("失败");
        }
    }
    //5 新增联系人
    @Override
    public ResultJson rankingnewaddContacts(User user, String firstSelect, String secondSelect) {
        Map<String,Object> map=new HashMap<>();
        ResultJson resultJson=new ResultJson();
        if(firstSelect!=null){
            if(firstSelect.equals("nowDay")){
                map.put("nowDay", DateUtil.dateFormatString(new Date(),"yyyy-MM-dd"));
            }else if(firstSelect.equals("nowMonth")){
                map.put("nowMonth", DateUtil.dateFormatString(new Date(),"yyyy-MM"));
            }else  if(firstSelect.equals("nowYear")){
                map.put("nowYear", DateUtil.dateFormatString(new Date(),"yyyy"));
            }else{
                map.put("s_time",firstSelect.split("--")[0]);
                map.put("e_time",firstSelect.split("--")[1]);
            }
        }
        if(secondSelect!=null){
            if(secondSelect!=null && secondSelect.equals("myself")){
                map.put("userId",user.getId());
            }else  if(secondSelect!=null && secondSelect.equals("myDepartment")){
                map.put("deptId",user.getDept_id());
            }
        }
        List<Map<String,Object>> mapList=moneyCensusMapper.rankingnewaddContacts(map);
        if(mapList!=null){
            return resultJson.getSuccess("成功",mapList);
        }else {
            return resultJson.getFail("失败");
        }
    }
    //6 新增商机
    @Override
    public ResultJson rankingnewaddOpportunity(User user, String firstSelect, String secondSelect) {
        Map<String,Object> map=new HashMap<>();
        ResultJson resultJson=new ResultJson();
        if(firstSelect!=null){
            if(firstSelect.equals("nowDay")){
                map.put("nowDay", DateUtil.dateFormatString(new Date(),"yyyy-MM-dd"));
            }else if(firstSelect.equals("nowMonth")){
                map.put("nowMonth", DateUtil.dateFormatString(new Date(),"yyyy-MM"));
            }else  if(firstSelect.equals("nowYear")){
                map.put("nowYear", DateUtil.dateFormatString(new Date(),"yyyy"));
            }else{
                map.put("s_time",firstSelect.split("--")[0]);
                map.put("e_time",firstSelect.split("--")[1]);
            }
        }
        if(secondSelect!=null){
            if(secondSelect!=null && secondSelect.equals("myself")){
                map.put("userId",user.getId());
            }else  if(secondSelect!=null && secondSelect.equals("myDepartment")){
                map.put("deptId",user.getDept_id());
            }
        }
        List<Map<String,Object>> mapList=moneyCensusMapper.rankingnewaddOpportunity(map);
        if(mapList!=null){
            return resultJson.getSuccess("成功",mapList);
        }else {
            return resultJson.getFail("失败");
        }
    }
    //7 新增商机金额
    @Override
    public ResultJson rankingnewaddOpportunityMoney(User user, String firstSelect, String secondSelect) {
        Map<String,Object> map=new HashMap<>();
        ResultJson resultJson=new ResultJson();
        if(firstSelect!=null){
            if(firstSelect.equals("nowDay")){
                map.put("nowDay", DateUtil.dateFormatString(new Date(),"yyyy-MM-dd"));
            }else if(firstSelect.equals("nowMonth")){
                map.put("nowMonth", DateUtil.dateFormatString(new Date(),"yyyy-MM"));
            }else  if(firstSelect.equals("nowYear")){
                map.put("nowYear", DateUtil.dateFormatString(new Date(),"yyyy"));
            }else{
                map.put("s_time",firstSelect.split("--")[0]);
                map.put("e_time",firstSelect.split("--")[1]);
            }
        }
        if(secondSelect!=null){
            if(secondSelect!=null && secondSelect.equals("myself")){
                map.put("userId",user.getId());
            }else  if(secondSelect!=null && secondSelect.equals("myDepartment")){
                map.put("deptId",user.getDept_id());
            }
        }
        List<Map<String,Object>> mapList=moneyCensusMapper.rankingnewaddOpportunityMoney(map);
        if(mapList!=null){
            return resultJson.getSuccess("成功",mapList);
        }else {
            return resultJson.getFail("失败");
        }
    }
    //8 新增根据记录
    @Override
    public ResultJson rankingnewaddFollow(User user, String firstSelect, String secondSelect) {
        Map<String,Object> map=new HashMap<>();
        ResultJson resultJson=new ResultJson();
        if(firstSelect!=null){
            if(firstSelect.equals("nowDay")){
                map.put("nowDay", DateUtil.dateFormatString(new Date(),"yyyy-MM-dd"));
            }else if(firstSelect.equals("nowMonth")){
                map.put("nowMonth", DateUtil.dateFormatString(new Date(),"yyyy-MM"));
            }else  if(firstSelect.equals("nowYear")){
                map.put("nowYear", DateUtil.dateFormatString(new Date(),"yyyy"));
            }else{
                map.put("s_time",firstSelect.split("--")[0]);
                map.put("e_time",firstSelect.split("--")[1]);
            }
        }
        if(secondSelect!=null){
            if(secondSelect!=null && secondSelect.equals("myself")){
                map.put("userId",user.getId());
            }else  if(secondSelect!=null && secondSelect.equals("myDepartment")){
                map.put("deptId",user.getDept_id());
            }
        }
        List<Map<String,Object>> mapList=moneyCensusMapper.rankingnewaddFollow(map);
        if(mapList!=null){
            return resultJson.getSuccess("成功",mapList);
        }else {
            return resultJson.getFail("失败");
        }
    }





    //旋转表的数据
    //合同
    @Override
    public ResultJson selectMoneyCensusRotating(User user, String firstSelect, String secondSelect) {
        Map<String,Object> map=new HashMap<>();
        ResultJson resultJson=new ResultJson();
        if(firstSelect!=null &&  firstSelect.equals("nowDay")){
            map.put("nowDay", DateUtil.dateFormatString(new Date(),"yyyy-MM-dd"));
        }else if(firstSelect!=null && firstSelect.equals("nowMonth")){
            map.put("nowMonth", DateUtil.dateFormatString(new Date(),"yyyy-MM"));
        }else if(firstSelect!=null && firstSelect.equals("nowYear")){
            map.put("nowYear",DateUtil.dateFormatString(new Date(),"yyyy-MM"));
        }else{   //2020-09-07--2020-09-08
            map.put("s_time",firstSelect.split("--")[0]);
            map.put("e_time",firstSelect.split("--")[1]);
        }
        if(secondSelect!=null && secondSelect.equals("myself")){
            map.put("userId",user.getId());
        }else  if(secondSelect!=null && secondSelect.equals("myDepartment")){
            map.put("deptId",user.getDept_id());
        }
        List<Map<String,Object>> mapList=moneyCensusMapper.selectMoneyCensusRotating(map);
        if(mapList!=null){
            return resultJson.getSuccess("成功",mapList);
        }else {
            return resultJson.getFail("失败");
        }

    }
    //回款
    @Override
    public ResultJson returnMoneyCensusRotating(User user, String firstSelect, String secondSelect) {
        Map<String,Object> map=new HashMap<>();
        ResultJson resultJson=new ResultJson();
        if(firstSelect!=null &&  firstSelect.equals("nowDay")){
            map.put("nowDay", DateUtil.dateFormatString(new Date(),"yyyy-MM-dd"));
        }else if(firstSelect!=null && firstSelect.equals("nowMonth")){
            map.put("nowMonth", DateUtil.dateFormatString(new Date(),"yyyy-MM"));
        }else if(firstSelect!=null && firstSelect.equals("nowYear")){
            map.put("nowYear",DateUtil.dateFormatString(new Date(),"yyyy-MM"));
        }else{   //2020-09-07--2020-09-08
            map.put("s_time",firstSelect.split("--")[0]);
            map.put("e_time",firstSelect.split("--")[1]);
        }
        if(secondSelect!=null && secondSelect.equals("myself")){
            map.put("userId",user.getId());
        }else  if(secondSelect!=null && secondSelect.equals("myDepartment")){
            map.put("deptId",user.getDept_id());
        }
        List<Map<String,Object>> mapList=moneyCensusMapper.returnMoneyCensusRotating(map);
        if(mapList!=null){
            return resultJson.getSuccess("成功",mapList);
        }else {
            return resultJson.getFail("失败");
        }
    }


}
